package com.bjc.base.config;

import com.alibaba.druid.pool.DruidDataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * mybatis 配置sqlserver 副数据源类
 * 
 * @author 许自辉
 * @date 2017年7月12日 下午5:31:14
 */
//@Configuration
//多数据源配置
//@MapperScan(basePackages = "com.bjc.user.daocluser", sqlSessionFactoryRef = "clusterSqlSessionFactory")
public class MybatisConfiguration2 implements EnvironmentAware {

//	private RelaxedPropertyResolver propertyResolver;

	private String driveClassName;
	private String url;
	private String userName;
	private String password;
	private static String xmlLocation = "classpath:mappercluser/*.xml";
	private String typeAliasesPackage = "com.bjc.user.daocluser";
	//////////////////////////////////////////////////////////////////////////

	@Bean(name = "clusterDataSource")
//	@ConfigurationProperties("spring.datasource.druid")
	public DataSource clusterDataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(driveClassName);
		dataSource.setUrl(url);
		dataSource.setUsername(userName);
		dataSource.setPassword(password);
		return dataSource;
	}

	@Bean(name = "clusterTransactionManager")
	public DataSourceTransactionManager clusterTransactionManager() {
		return new DataSourceTransactionManager(clusterDataSource());
	}

	@Bean(name = "clusterSqlSessionFactory")
	public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("clusterDataSource") DataSource clusterDataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(clusterDataSource);
		bean.setTypeAliasesPackage(typeAliasesPackage);
		// 分页插件
//		PageInterceptor interceptor = new PageInterceptor();
//		Properties properties = new Properties();
//		properties.setProperty("helperDialect", "sqlserver");
//		properties.setProperty("reasonable", "true");
//		properties.setProperty("supportMethodsArguments", "true");
//		properties.setProperty("returnPageInfo", "check");
//		properties.setProperty("params", "count=countSql");
//		interceptor.setProperties(properties);
//		PageInterceptor[] plugins = new PageInterceptor[] { interceptor };
//		bean.setPlugins(plugins);
		
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		bean.setMapperLocations(resolver.getResources(xmlLocation));
		return bean.getObject();
	}
 
	@Override
	public void setEnvironment(Environment environment) {
//		this.propertyResolver = new RelaxedPropertyResolver(environment, null);
//		this.url = propertyResolver.getProperty("cluster.datasource.url");
//		this.userName = propertyResolver.getProperty("cluster.datasource.username");
//		this.password = propertyResolver.getProperty("cluster.datasource.password");
//		this.driveClassName = propertyResolver.getProperty("cluster.datasource.driver-class-name");
		// this.typeAliasesPackage =
		// propertyResolver.getProperty("mybatis.typeAliasesPackage");
		// this.xmlLocation =
		// propertyResolver.getProperty("mybatis.xmlLocation");
	}

}
